package com.fable.sp.core.widget;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.common.hash.Hashing;

import java.nio.charset.Charset;

/**
 * .
 *
 * @author stormning on 16/8/15.
 */
public class Preference {

    private String name;

    private String displayName;

    private String value;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }


    public <T> T getValue(Class<T> clazz) {
        return JSON.parseObject(getValue(), clazz);
    }

    public <T> T getValue(TypeReference<T> typeReference) {
        return JSON.parseObject(getValue(), typeReference);
    }

    public JSONObject getJSONObject() {
        return JSON.parseObject(getValue());
    }

    public JSONArray getJSONArray() {
        return JSON.parseArray(getValue());
    }

    public Preference() {
    }

    public Preference(String name, String displayName, String value) {
        this.name = name;
        this.displayName = displayName;
        this.value = value;
    }

    @Override
    public int hashCode() {
        return Hashing.sipHash24().hashString(getName(), Charset.forName("UTF-8")).asInt();
    }

    @Override
    public boolean equals(Object obj) {
        return hashCode() == obj.hashCode();
    }

    @Override
    public String toString() {
        return value;
    }
}
